package com.exartisan.thanhdoanhn;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

import com.exartisan.adapter.PostAdapter;
import com.exartisan.bean.Post;
import com.exartisan.network.NetworkUtility;
import com.exartisan.network.ParametersBuilder;
import com.exartisan.network.ResponseTranslater;
import com.exartisan.network.RestConnector;
import com.loopj.android.http.AsyncHttpResponseHandler;

public class EventContentActivity extends ListActivity {

	private PostAdapter mAdapter;
	private ArrayList<Post> posts;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_event_content);
		posts = new ArrayList<Post>();
		//showData();
		getData();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.event_content, menu);
		return true;
	}

	
	private void getData() {
		if (NetworkUtility.checkNetworkState(this)) {
			RestConnector.post("", ParametersBuilder
					.BuildParams(ParametersBuilder.BuildPostData(4, 1)),
					new AsyncHttpResponseHandler() {
						@Override
						public void onSuccess(int arg0, String response) {
							super.onSuccess(arg0, response);
							if (ResponseTranslater.checkSuccess(response)) {
								posts = ResponseTranslater.getPosts(response);
								mAdapter = new PostAdapter(getBaseContext(),
										posts);
								setListAdapter(mAdapter);

							} else {
								Toast.makeText(getBaseContext(),
										"Không có dữ liệu", Toast.LENGTH_SHORT)
										.show();
								finish();
							}

						}

						@Override
						public void onFailure(Throwable arg0, String arg1) {
							super.onFailure(arg0, arg1);
							Toast.makeText(getBaseContext(),
									"Không có dữ liệu", Toast.LENGTH_SHORT)
									.show();
							finish();
						}
					});
		} else {
			Toast.makeText(getBaseContext(), "Không có kết nối",
					Toast.LENGTH_SHORT).show();
			finish();
		}
	}

	public void handleGallaryNormal(View v) {
		Intent intent = new Intent(this, GalleryActivity.class);
		startActivity(intent);
	}

}
